package web2023.team.homework_10_aop.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import web2023.team.homework_10_aop.entity.Login_info;
import web2023.team.homework_10_aop.entity.User;
import web2023.team.homework_10_aop.service.LoginInfoServiceImpl;
import web2023.team.homework_10_aop.service.UserService;

import javax.servlet.http.HttpServletRequest;
import java.util.List;



@Controller
public class UserController {

    @Autowired
    UserService userService;
    //UserService userService = new UserServiceImpl();

    @RequestMapping("/login")
    public String login(){
        return "login";
    }
    // 登录
    @RequestMapping("/userLogin")
    public String userlogin (@RequestParam("username") String username,
                        @RequestParam("password") String password,
                         HttpServletRequest request,
                         Model model) {
        boolean result = userService.userLogin(new User(null,username,password));
        if(result){
            request.getSession().setAttribute("user",username);
            return "main";
        }
        else
            return "login";
    }

    @Autowired
    LoginInfoServiceImpl loginInfoService;
    // 返回日志记录
    @RequestMapping(path = "/allLoginInfo", method = RequestMethod.GET, produces = "application/json")
    @ResponseBody
    public List<Login_info> allLoginInfo (HttpServletRequest request,
                                                       Model model) {
        return loginInfoService.getAllLoginInfo();
    }
}
